package cate.game.client.msg;

import cate.game.client.netty.NettyContext;
import easy.java.codec.JsonCodec;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GameMsg {

    /** 编码解码方式:无内容 */
    public final static byte CODEC_NULL = 0;
    /** 编码解码方式:JSON (BinaryWebSocketFrame) */
    public final static byte CODEC_JSON = 1;
    /** 编码解码方式:JSON-ZIP (BinaryWebSocketFrame) */
    public final static byte CODEC_JSON_ZIP = 2;
    /** 编码解码方式: (TextWebSocketFrame) */
    public final static byte CODEC_TEXT_FRAME = 8;

    public NettyContext context;

    public int idA;

    public int idB;

    public int srvId;

    public byte codec;

    public byte[] contentBytes;

    public <T> T decodeJson(Class<T> clazz) {
        try {
            if(this.contentBytes == null || this.contentBytes.length == 0) {
                return clazz.getDeclaredConstructor().newInstance();
            }
            return JsonCodec.instance().decode(this.contentBytes, clazz);
        } catch (Exception e) {
            log.error("消息json反序列化时出错", e);
            return null;
        }
    }

}
